package com.hfy.pcberpbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hfy.pcberpbackend.entity.Material;
import com.hfy.pcberpbackend.mapper.MaterialMapper;
import com.hfy.pcberpbackend.service.MaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.StrUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 物料Service实现类
 */
@Slf4j
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {
    
    @Override
    public List<Material> searchMaterials(String keyword, Integer size) {
        log.info("模糊搜索物料，关键词：{}，数量限制：{}", keyword, size);
        
        if (StrUtil.isBlank(keyword)) {
            return new ArrayList<>();
        }
        
        LambdaQueryWrapper<Material> wrapper = new LambdaQueryWrapper<>();
        wrapper.and(qw -> qw
                .like(Material::getMaterialCode, keyword)
                .or()
                .like(Material::getMaterialName, keyword)
        );
        wrapper.eq(Material::getStatus, "active");
        wrapper.orderByDesc(Material::getUpdatedAt);
        
        if (size != null && size > 0) {
            wrapper.last("LIMIT " + size);
        }
        
        return list(wrapper);
    }
} 